package com.lingshi.shopping_category_service.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingshi.shopping_category_service.mapper.CategoryMapper;
import com.lingshi.shopping_common.constant.RedisKey;
import com.lingshi.shopping_common.entity.Category;
import com.lingshi.shopping_common.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;


import java.util.Arrays;
import java.util.List;

@DubboService
@Slf4j
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    RedisTemplate redisTemplate;


    @Override
    public void add(Category category) {
        categoryMapper.insert(category);
        refreshRedisCategory();
    }

    @Override
    public void update(Category category) {
        categoryMapper.updateById(category);
        refreshRedisCategory();
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Category category = categoryMapper.selectById(id);
        category.setStatus(status);
        categoryMapper.updateById(category);
        refreshRedisCategory();
    }

    @Override
    public void delete(Long[] ids) {
        categoryMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public Category findById(Long id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public Page<Category> search(int page, int size) {
        return categoryMapper.selectPage(new Page<>(page,size),null);
    }

    @Override
    public List<Category> findAll() {
        ListOperations<String,Category> los = redisTemplate.opsForList();
        List<Category> categorys = los.range(RedisKey.CATRGORYS, 0, -1);
        if(CollUtil.isNotEmpty(categorys)){
            log.info("=========从redis查询数据");
            return categorys;
        }else {
            log.info("=========从数据库查询数据");
            // 从数据库查询所有启用的广告
            LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Category::getStatus, 1);
            List<Category> categories = categoryMapper.selectList(queryWrapper);
            return categories;
        }
    }

    /**
     * 更新redis中的广告数据
     */
    public void refreshRedisCategory(){
        // 从数据库查询广告
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getStatus,1);
        List<Category> categories = categoryMapper.selectList(queryWrapper);


        // 删除redis中的原有广告数据
        redisTemplate.delete(RedisKey.CATRGORYS);
        // 将新的广告数据同步到redis中
        ListOperations<String,Category> listOperations = redisTemplate.opsForList();
        listOperations.leftPushAll(RedisKey.CATRGORYS,categories);
    }

}
